package com.lighting.module3;

import java.text.NumberFormat;
import java.util.Locale;

//等额本息 计算器
public class Calculator0 extends Calculator {
    //字段
//    double p;   //本金
//    int m;      //月份
//    double yr;  //年利率

    //构造方法
    public Calculator0(double p, int m, double yr) {
        super(p, m, yr);
    }




    //普通方法
    @Override
    public String[] cal() {
        double mr = yr / 12.0 / 100.0;        //mr为月利率
        double pow = Math.pow(1 + mr, m);
        double payment = p * mr * pow / (pow - 1);

        String[] arr = new String[]{
                NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment * m),
                NumberFormat.getCurrencyInstance(Locale.CHINA).format(payment * m - p)
        };
        return arr;
    }

    @Override
    public String[][] details() {
        String[][] a2 = new String[m][];

        double mr = yr / 12.0 / 100.0;        //mr为月利率
        double pow = Math.pow(1 + mr, m);
        double payment = p * mr * pow / (pow - 1); //月供

        for (int i = 0; i < m; i++) {
            double payInterest = p * mr;                  //偿还利息
            double payPrincipal = payment - payInterest;  //偿还本金
            p = p - payPrincipal;                         //剩余本金

            a2[i] = CreateRow(i, payment, payPrincipal, payInterest);
        }
        return a2;
    }
}
